package com.blogManager.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.blogManager.entity.PageResult;
import com.blogManager.entity.Result;
import com.blogManager.pojo.TbBlog;
import com.blogManager.service.BlogService;

@Controller
@RequestMapping("/blog")
public class BlogController {

	@Autowired
	private BlogService blogService;

	@RequestMapping("/getBlogList")
	@ResponseBody
	public PageResult getBlogList(int page, int rows) {
		return blogService.getBlogList(page, rows);
	}

	@RequestMapping("/search")
	@ResponseBody
	public PageResult search(int page, int rows, @RequestBody TbBlog blog) {
		return blogService.search(page, rows, blog);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Result delete(int ids[]) {
		try {
			blogService.delete(ids);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}

	@RequestMapping("/upload")
	@ResponseBody
	public String upload(MultipartFile uploadFile, HttpSession session) {
		try {
			
			//文件命名空间
			String blog_pic = "D:/blog_pic";
			//linux
			//String blog_pic = "/blog_pic";
			String filename = uploadFile.getOriginalFilename();
			//数据库存储的文件路径
			String filePathDB = "/content_pics/"+filename;
			
			//最终文件路径
			String finalPath = blog_pic + filePathDB;
			
			//String realPath = session.getServletContext().getRealPath("/images");
			File file = new File(finalPath);
			uploadFile.transferTo(file);
			JSONObject obj = new JSONObject();
			obj.put("error",0);
			obj.put("url",filePathDB);
			return obj.toJSONString();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Result add(@RequestBody TbBlog blog) {
		try {
			blogService.add(blog);
			return new Result(true, "添加成功");
		} catch (Exception e) {
			
			e.printStackTrace();
			return new Result(false,"添加失败，请检查表单");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Result update(@RequestBody TbBlog blog) {
		try {
			blogService.update(blog);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			
			e.printStackTrace();
			return new Result(false,"修改失败,请检查表单");
		}
	}
	
	@RequestMapping("/findBlogById")
	@ResponseBody
	public TbBlog findBlogById(int blogId) {
		return blogService.findBlogById(blogId);
	}
	
	@RequestMapping("/upImgFile")
	@ResponseBody
	public String upImgFile(MultipartFile image,HttpServletRequest request) {
		try {
			

			//文件命名空间
			//String blog_pic = "/blog_pic";
			String blog_pic = "D:/blog_pic";
			String filename = image.getOriginalFilename();
			//数据库存储的文件路径
			String filePathDB = "/title_pics/"+filename;
			
			//最终文件路径
			String finalPath = blog_pic + filePathDB;
			//应用名称
			String appName = request.getContextPath();
			//String realPath = request.getSession().getServletContext().getRealPath("/images");
			File file = new File(finalPath);
			image.transferTo(file);
			return filePathDB;
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return "fail";
		} catch (IOException e) {
			e.printStackTrace();
			return "fail";
		}
		
	}
	
}
